package com.zzr.demo.redis.II.controller;

import com.zzr.demo.redis.II.service.MessageQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author: xiaomi
 * @date: 2021/10/11
 * @description:
 */
@RestController
@RequestMapping("mq/stream")
@Api(tags = "消息队列：使用 Stream 实现")
@Slf4j
public class MessageQueueByStreamController {

    @Autowired
    MessageQueueService messageQueueService;

    @GetMapping("/addMessage/{key}/{value}")
    @ApiOperation("生产消息")
    public String addMessage(@PathVariable("key") String key, @PathVariable("value") String value) {
        String recordId = messageQueueService.addMessage(key, value);
        return recordId;
    }


    @GetMapping("/getMessage/{recordId}")
    @ApiOperation("获取消息")
    public String getMessage(@PathVariable("recordId") String recordId) {
        String message = messageQueueService.getMessage(recordId);
        return message;
    }


    @GetMapping("/size")
    @ApiOperation("获取消息的数量（ID 的个数）")
    public long size() {
        long size = messageQueueService.size();
        return size;
    }


    @GetMapping("/createGroup/{name}")
    @ApiOperation("创建消费者组")
    public boolean createGroup(@PathVariable("name") String name) {
        messageQueueService.createGroup(name);
        return true;
    }

    @GetMapping("/destroyGroup/{name}")
    @ApiOperation("删除消费者组")
    public boolean destroyGroup(@PathVariable("name") String name) {
        boolean b = messageQueueService.destroyGroup(name);
        return b;
    }

    @GetMapping("/readGroup/{groupName}/{consumerName}")
    @ApiOperation("从消费者组读取信息")
    public Set<String> readGroup(@PathVariable("groupName") String groupName, @PathVariable("consumerName") String consumerName) {
        Set<String> strings = messageQueueService.readGroup(groupName, consumerName);
        return strings;
    }


    @GetMapping("/ack/{groupName}/{recordId}")
    @ApiOperation("确认消费者中消费者的消息")
    public boolean ack(@PathVariable("groupName") String groupName, @PathVariable("recordId") String recordId) {
        boolean ack = messageQueueService.ack(groupName, recordId);
        return ack;
    }
}
